package com.enjoyor.soa.traffic.server.efoms.dubbo.manage;

import com.alibaba.dubbo.config.annotation.Service;
import com.enjoyor.soa.traffic.api.dto.efoms.WranTacticsInfoDto;
import com.enjoyor.soa.traffic.core.efoms.domain.WranTacticsInfo;
import com.enjoyor.soa.traffic.core.efoms.service.IWranTacticsService;
import com.enjoyor.soa.traffic.util.enums.EnumAppCode;
import com.enjoyor.soa.traffic.util.helper.LoggerHelper;
import com.enjoyor.soa.traffic.util.helper.ReflectHelper;
import com.enjoyor.soa.traffic.util.helper.ResultHelper;
import com.enjoyor.soa.traffic.util.pojo.ResultPojo;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

@Service
public class ManageWranTacticsService {

    @Autowired
    private IWranTacticsService wranTacticsService;

    /**
     * 根据策略编号查询设备策略信息
     * @param tacticsId 策略编号
     * @return ResultPojo
     */
    public ResultPojo selectWranTacticsById(String tacticsId){
    	try {
    		WranTacticsInfo wranTacticsInfo = wranTacticsService.selectWranTacticsById(tacticsId);
    		WranTacticsInfoDto wranTacticsInfoDto = ReflectHelper.beanToBean(wranTacticsInfo, WranTacticsInfoDto.class);;
	        return ResultHelper.getResult(wranTacticsInfoDto);
    	} catch (Exception e) {
            LoggerHelper.LOG.error(e);
            return ResultHelper.getResult(e);
        }
    }
    
    /**
     * 查询策略列表信息
     * @param tacticsId 策略编号
     * @param tacticsLevelCode 策略等级编号
     * @param wranTypeCode 报警类型编号
     * @param wranLevelCode 报警等级编号
     * @param devTypeCode 设备类型编号
     * @param devId 设备编号
     * @return ResultPojo
     */
	public ResultPojo selectWranTacticsList(String tacticsId, String tacticsLevelCode, 
			String wranTypeCode, String wranLevelCode, String devTypeCode, String devId) {
        try {
			List<WranTacticsInfo> wranTacticsInfoList = wranTacticsService.selectWranTacticsList(tacticsId,
					tacticsLevelCode, wranTypeCode, wranLevelCode, devTypeCode, devId);
			List<WranTacticsInfoDto> wranTacticsInfoDtoList = ReflectHelper.listToList(wranTacticsInfoList,
					WranTacticsInfoDto.class);
			return ResultHelper.getResult(wranTacticsInfoDtoList);
        } catch (Exception e) {
            LoggerHelper.LOG.error(e);
            return ResultHelper.getResult(e, EnumAppCode.OPERATOR_FAILURE);
        }
    }
    
	/**
     * 查询对应报警是否存在策略--server后台使用
     * @param wranTypeCode 报警类型编号
     * @param devTypeCode 设备类型编号
     * @param devId 设备编号
     * @return ResultPojo
     */
	public List<WranTacticsInfo> selectWranTacticsList(String wranTypeCode,String devTypeCode, String devId) {
        try {
			List<WranTacticsInfo> wranTacticsInfoList = wranTacticsService.selectWranTacticsList(null,
					null, wranTypeCode, null, devTypeCode, devId);
			return wranTacticsInfoList;
        } catch (Exception e) {
            LoggerHelper.LOG.error(e);
            return null;
        }
    }
	
    /**
     * 新增策略信息
     * @param wranTacticsInfoDto
     * @param username 操作人
     * @return ResultPojo
     */
    public ResultPojo insertWranTacticsInfo(WranTacticsInfoDto wranTacticsInfoDto, String username) {
        try {
            if (wranTacticsInfoDto == null)
                return ResultHelper.getResult(EnumAppCode.PARAM_ERR);
            WranTacticsInfo wranTacticsInfo = ReflectHelper.beanToBean(wranTacticsInfoDto, WranTacticsInfo.class);
            String tacticsId = wranTacticsService.insertWranTacticsInfo(wranTacticsInfo, username);
            return ResultHelper.getResult(tacticsId);
        } catch (Exception e) {
            LoggerHelper.LOG.error(e);
            return ResultHelper.getResult(e, EnumAppCode.OPERATOR_FAILURE);
        }
    }

    /**
     * 修改策略信息
     * @param wranTacticsInfoDto
     * @param username 操作人 
     * @return ResultPojo
     */
    public ResultPojo updateWranTacticsInfo(WranTacticsInfoDto wranTacticsInfoDto, String username) {
        try {
            if (wranTacticsInfoDto == null || wranTacticsInfoDto.getTacticsId() == null || 
            		wranTacticsInfoDto.getTacticsId().equals(""))
            	return ResultHelper.getResult(EnumAppCode.PARAM_ERR);
            WranTacticsInfo wranTacticsInfo = ReflectHelper.beanToBean(wranTacticsInfoDto, WranTacticsInfo.class);
            String tacticsId = wranTacticsService.updateWranTacticsInfo(wranTacticsInfo, username);
            return ResultHelper.getResult(tacticsId);
        } catch (Exception e) {
            LoggerHelper.LOG.error(e);
            return ResultHelper.getResult(e, EnumAppCode.OPERATOR_FAILURE);
        }

    }

    /**
     * 删除策略信息
     * @param tacticsId 策略编号
     * @param username 操作人
     * @return ResultPojo
     */
    public ResultPojo deleteWranTacticsInfo(String tacticsId, String username) {
        try {
            if (tacticsId == null || tacticsId.equals(""))
                return ResultHelper.getResult(EnumAppCode.PARAM_ERR);
            wranTacticsService.deleteWranTacticsInfo(tacticsId, username);
            return ResultHelper.getResult(tacticsId);
        } catch (Exception e) {
            LoggerHelper.LOG.error(e);
            return ResultHelper.getResult(e, EnumAppCode.OPERATOR_FAILURE);
        }
    }
}
